#!/bin/bash
set -e  # 出现任何错误立即退出
set -o pipefail

TARGET_DIR="/opt/zoral"

# 确认提示
echo "This will remove and reinstall files under $TARGET_DIR"
read -p "Are you sure you want to continue? [y/N]: " confirm
if [[ "$confirm" != "y" && "$confirm" != "Y" ]]; then
    echo "Cancelled."
    exit 1
fi

# 删除目标目录
if [ -d "$TARGET_DIR" ]; then
    echo "Removing existing $TARGET_DIR..."
    sudo rm -rf "$TARGET_DIR"
fi

# 创建目标目录
echo "Creating $TARGET_DIR..."
sudo mkdir -p "$TARGET_DIR"

# 复制 third_party 和 toolchain
echo "Copying all files..."
sudo cp -r x86_64/ "$TARGET_DIR/"
sudo cp -r aarch64/ "$TARGET_DIR/"
sudo cp -r data/ "$TARGET_DIR/"

echo "Installation completed successfully."
